package com.mybatis.jpa.SqlProvider;


import com.mybatis.jpa.meta.MetaCell;
import org.apache.ibatis.builder.annotation.ProviderContext;
import org.apache.ibatis.jdbc.SQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @Author lgt
 * @Date 2017/10/10 : 21:00
 */
public class DeleteSqlProvider {

    private static final Logger logger = LoggerFactory.getLogger(DeleteSqlProvider.class);

    public String deleteByPrimaryKey(Map<String,Object> map, ProviderContext context) {
        Check.notNull(map.get("pk"), "pk must not be null");
        MetaCell meta = ProviderUtil.getMetaCellByMapper(context);
        SQL sql = new SQL();
        sql.DELETE_FROM(meta.getTableName());
        sql.WHERE(meta.getPrimaryKey() + " = #{pk}");
        logger.debug("deleteByPrimaryKey : {}", sql.toString());
        return sql.toString();
    }

    public String deleteByCondition(Condition condition , ProviderContext context){
        Check.notNull(condition, "condition must not be null");
        MetaCell meta = ProviderUtil.getMetaCellByMapper(context);
        SQL sql = new SQL();
        sql.DELETE_FROM(meta.getTableName());
        logger.debug("deleteByCondition : {} {}", sql.toString() , condition.toString());
        return sql.toString() + condition.toString();
    }

}
